package com.dubito.tmdbmovie.ui.navigation

import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.navigation.NavController
import androidx.navigation.NavHostController
import androidx.navigation.NavType
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.navArgument
import com.dubito.tmdbmovie.R
import com.dubito.tmdbmovie.ui.screen.bottom.navigation.nowplaying.NowPlaying
import com.dubito.tmdbmovie.ui.screen.moviedetail.MovieDetailScreen
import com.dubito.tmdbmovie.ui.screen.artistdetail.ArtistDetailScreen
import com.dubito.tmdbmovie.ui.screen.bottom.navigation.popular.PopularScreen
import com.dubito.tmdbmovie.ui.screen.bottom.navigation.toprated.TopRatedScreen
import com.dubito.tmdbmovie.ui.screen.bottom.navigation.upcoming.UpcomingScreen

@Composable
fun Navigation(
    navController: NavHostController,
    bottomBarVisibility: MutableState<Boolean>,
    isAppBarVisible: MutableState<Boolean>,
    modifier: Modifier = Modifier
) {
    NavHost(navController, startDestination = "home") {
        composable(NavigationScreen.HOME) {
            LaunchedEffect(key1 = null) {
                bottomBarVisibility.value = true
            }
            NowPlaying(
                navController = navController,
                modifier = modifier
            )
        }
//        composable(NavigationScreen.LOGIN) {
//            Login(
//                navController = navController
//            )
//        }
        composable(NavigationScreen.POPULAR) {
            LaunchedEffect(key1 = null) {
                bottomBarVisibility.value = true
            }
            PopularScreen(
                navController = navController,
                modifier = modifier
            )
        }
        composable(NavigationScreen.TOP_RATED) {
            LaunchedEffect(key1 = null) {
                bottomBarVisibility.value = true
            }
            TopRatedScreen(
                navController = navController,
                modifier = modifier
            )
        }
        composable(NavigationScreen.UP_COMING) {
            LaunchedEffect(key1 = null) {
                bottomBarVisibility.value = true
            }
            UpcomingScreen(
                navController = navController,
                modifier = modifier
            )
        }
//        composable(
//            NavigationScreen.NAVIGATION_DRAWER_WITH_GENRE_ID,
//            arguments = listOf(navArgument(NavigationScreen.GENRE_ID) {
//                type = NavType.StringType
//            })
//        ) { backStack ->
//            val genreId = backStack.arguments?.getString(NavigationScreen.GENRE_ID)
//            genreId?.let {
//                GenreScreen(
//                    navController = navController,
//                    genreId
//                )
//            }
//        }
        composable(
            NavigationScreen.MovieDetail.MOVIE_DETAIL.plus(NavigationScreen.MovieDetail.MOVIE_DETAIL_PATH),
            arguments = listOf(navArgument(NavigationScreen.MovieDetail.MOVIE_ITEM) {
                type = NavType.IntType
            })
        ) {
            LaunchedEffect(key1 = null) {
                bottomBarVisibility.value = false
                isAppBarVisible.value = true
            }
            label = stringResource(R.string.movie_detail)
            val movieId =
                it.arguments?.getInt(NavigationScreen.MovieDetail.MOVIE_ITEM)
            if (movieId != null) {
                MovieDetailScreen(
                    navController = navController,
                    movieId
                )
            }
        }
        composable(
            NavigationScreen.ArtistDetail.ARTIST_DETAIL.plus(NavigationScreen.ArtistDetail.ARTIST_DETAIL_PATH),
            arguments = listOf(navArgument(NavigationScreen.ArtistDetail.ARTIST_ID) {
                type = NavType.IntType
            })
        ) {
            label = stringResource(R.string.artist_detail)
            val artistId =
                it.arguments?.getInt(NavigationScreen.ArtistDetail.ARTIST_ID)
            if (artistId != null) {
                ArtistDetailScreen(
                    artistId
                )
            }
        }
    }
}

@Composable
fun navigationTitle(navController: NavController): String {
    return when (currentRoute(navController)) {
        NavigationScreen.MovieDetail.MOVIE_DETAIL -> stringResource(id = R.string.movie_detail)
        NavigationScreen.ArtistDetail.ARTIST_DETAIL -> stringResource(id = R.string.artist_detail)
        NavigationScreen.LOGIN -> stringResource(id = R.string.login)
        else -> {
            ""
        }
    }
}

@Composable
fun currentRoute(navController: NavController): String? {
    val navBackStackEntry by navController.currentBackStackEntryAsState()
    return navBackStackEntry?.destination?.route?.substringBeforeLast("/")
}
